package com.cyf.mq.producer;

import com.cyf.mq.constant.MqConstant;
import com.cyf.mq.pojo.HeroDTO;
import com.cyf.mq.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @author chenyifan
 * @create 2025-02-18 15:46
 */
@Slf4j
public class AsyncProducer {

    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.MESSAGE_PRODUCER_GROUP_A);
        producer.setNamesrvAddr(MqConstant.NAME_SERVER_ADDR);
        producer.start();

        HeroDTO heroDTO = new HeroDTO("jialuo", "伽罗");
        org.apache.rocketmq.common.message.Message message = new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO).getBytes());

        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送成功：{}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                log.error("发送失败", e);
            }
        });
        log.info("我先执行");
        System.in.read();
    }
}